|
1
|
|
|
// Encoding documentation: |
|
2
|
|
|
// https://en.wikipedia.org/wiki/Code_39#Encoding |
|
3
|
|
|
|
|
4
|
|
|
class CODE39 { |
|
5
|
|
|
constructor(string, options){ |
|
6
|
|
|
this.string = string.toUpperCase(); |
|
7
|
|
|
|
|
8
|
|
|
// Enable mod43 checksum? |
|
9
|
|
|
this.mod43Enabled = options.mod43 || false; |
|
10
|
|
|
|
|
11
|
|
|
// All characters. The position in the array is the (checksum) value |
|
12
|
|
|
this.characters = [ |
|
13
|
|
|
"0", "1", "2", "3", |
|
14
|
|
|
"4", "5", "6", "7", |
|
15
|
|
|
"8", "9", "A", "B", |
|
16
|
|
|
"C", "D", "E", "F", |
|
17
|
|
|
"G", "H", "I", "J", |
|
18
|
|
|
"K", "L", "M", "N", |
|
19
|
|
|
"O", "P", "Q", "R", |
|
20
|
|
|
"S", "T", "U", "V", |
|
21
|
|
|
"W", "X", "Y", "Z", |
|
22
|
|
|
"-", ".", " ", "$", |
|
23
|
|
|
"/", "+", "%", "*" |
|
24
|
|
|
]; |
|
25
|
|
|
|
|
26
|
|
|
// The decimal representation of the characters, is converted to the |
|
27
|
|
|
// corresponding binary with the getEncoding function |
|
28
|
|
|
this.encodings = [ |
|
29
|
|
|
20957, 29783, 23639, 30485, |
|
30
|
|
|
20951, 29813, 23669, 20855, |
|
31
|
|
|
29789, 23645, 29975, 23831, |
|
32
|
|
|
30533, 22295, 30149, 24005, |
|
33
|
|
|
21623, 29981, 23837, 22301, |
|
34
|
|
|
30023, 23879, 30545, 22343, |
|
35
|
|
|
30161, 24017, 21959, 30065, |
|
36
|
|
|
23921, 22385, 29015, 18263, |
|
37
|
|
|
29141, 17879, 29045, 18293, |
|
38
|
|
|
17783, 29021, 18269, 17477, |
|
39
|
|
|
17489, 17681, 20753, 35770 |
|
40
|
|
|
]; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
// Get the binary representation of a character by converting the encodings |
|
44
|
|
|
// from decimal to binary |
|
45
|
|
|
getEncoding(character){ |
|
46
|
|
|
return this.getBinary(this.characterValue(character)); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
getBinary(characterValue){ |
|
50
|
|
|
return this.encodings[characterValue].toString(2); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
getCharacter(characterValue){ |
|
54
|
|
|
return this.characters[characterValue]; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
characterValue(character){ |
|
58
|
|
|
return this.characters.indexOf(character); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
|
|
62
|
|
|
encode(){ |
|
63
|
|
|
var string = this.string; |
|
64
|
|
|
|
|
65
|
|
|
// First character is always a * |
|
66
|
|
|
var result = this.getEncoding("*"); |
|
67
|
|
|
|
|
68
|
|
|
// Take every character and add the binary representation to the result |
|
69
|
|
|
for(let i = 0; i < this.string.length; i++){ |
|
70
|
|
|
result += this.getEncoding(this.string[i]) + "0"; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
// Calculate mod43 checksum if enabled |
|
74
|
|
|
if(this.mod43Enabled){ |
|
75
|
|
|
var checksum = this.mod43checksum(); |
|
76
|
|
|
result += this.getBinary(checksum) + "0"; |
|
77
|
|
|
string += this.getCharacter(checksum); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
// Last character is always a * |
|
81
|
|
|
result += this.getEncoding("*"); |
|
82
|
|
|
|
|
83
|
|
|
return { |
|
84
|
|
|
data: result, |
|
85
|
|
|
text: string |
|
86
|
|
|
}; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
valid(){ |
|
90
|
|
|
return this.string.search(/^[0-9A-Z\-\.\ \$\/\+\%]+$/) !== -1; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
mod43checksum(){ |
|
94
|
|
|
var checksum = 0; |
|
95
|
|
|
for(let i = 0; i < this.string.length; i++){ |
|
96
|
|
|
checksum += this.characterValue(this.string[i]); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
checksum = checksum % 43; |
|
100
|
|
|
return checksum; |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
export {CODE39}; |
|
105
|
|
|
|